<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 
 */
class Role_Model extends MY_Model
{
	protected $table = 'role';

	public function add()
	{
		$data = $this->get_data($this->table);
		if (empty($data)) {
			return 0;
		}
		$data['update_time'] = $data['create_time'] = time();
		$data['status'] = empty($data['status']) ? 0 : 1;
		if (empty($data['auth'])) {
			$data['auth'] = [];
		}
		$data['auth'] = json_encode($data['auth']);
		$this->db->insert($this->table, $data);
		return $this->db->insert_id();
	}

	public function lists($where = [], $select = [], $order_by = 'sort ASC, id ASC')
	{
		return $this->db
			->select($select)
			->order_by($order_by)
			->get_where($this->table, $where)
			->result();
	}

	public function row($where = [], $select = [])
	{
		if (empty($where)) {
			return [];
		}
		return $this->db
			->select($select)
			->get_where($this->table, $where)
			->row();
	}

	public function edit($where = [])
	{
		if (empty($where)) {
			return 0;
		}
		$data = $this->get_data($this->table);
		if (empty($data)) {
			return 0;
		}
		$data['update_time'] = time();
		$data['status'] = empty($data['status']) ? 0 : 1;
		if (empty($data['auth'])) {
			$data['auth'] = [];
		}
		$data['auth'] = json_encode($data['auth']);
		$this->db->update($this->table, $data, $where);
		return $this->db->affected_rows();
	}

	public function del($where = [])
	{
		if (empty($where)) {
			return 0;
		}
		$this->db->delete($this->table, $where);
		return $this->db->affected_rows();
	}
}
